/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.spakehelpdesk.bean;

import com.spakehelpdesk.entity.NewType;
import com.spakehelpdesk.entity.News;
import com.spakehelpdesk.java.FileUpLoad;
import com.spakehelpdesk.java.MessageManage;
import com.spakehelpdesk.java.SessionManage;
import com.spakehelpdesk.java.URLManage;
import com.spakehelpdesk.session.NewTypeSession;
import com.spakehelpdesk.session.NewsSession;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.primefaces.event.FileUploadEvent;

/**
 *
 * @author leehoanh
 */
@SessionScoped
public class NewsBean {

    @EJB
    private NewTypeSession newTypeSession;
    @EJB
    private NewsSession newsSession;
    private List<News> listNews;
    private News selectNews = new News();
    private News newNews = new News();
    private int typeNewid = 2;
    private FileUpLoad file = new FileUpLoad();
    private Integer newId;

    public Integer getNewId() {
        return newId;
    }

    public void setNewId(Integer newId) {
        this.newId = newId;
    }

    public FileUpLoad getFile() {
        return file;
    }

    public void setFile(FileUpLoad file) {
        this.file = file;
    }
    /**
     * Creates a new instance of NewsBean
     */
    private List<News> newsList = new ArrayList<News>();
    private News news = new News();

    public int getTypeNewid() {
        return typeNewid;
    }

    public void setTypeNewid(int typeNewid) {
        this.typeNewid = typeNewid;
    }

    public News getNewNews() {
        return newNews;
    }

    public void setNewNews(News newNews) {
        this.newNews = newNews;
    }
    private SelectItem[] type = null;

    public SelectItem[] getType() {
        return type;
    }

    public void setType(SelectItem[] type) {
        this.type = type;
    }

    public NewsSession getNewsSession() {
        return newsSession;
    }

    public void setNewsSession(NewsSession newsSession) {
        this.newsSession = newsSession;
    }

    public News getSelectNews() {
        return selectNews;
    }

    public void setSelectNews(News selectNews) {
        this.selectNews = selectNews;
    }

    public SelectItem[] getListType() {
        List<NewType> types = newTypeSession.getAll();
        if (types != null) {
            type = new SelectItem[types.size() + 1];
            type[0] = new SelectItem("", "--Select--");
            for (int i = 0; i < types.size(); i++) {
                type[i + 1] = new SelectItem(types.get(i).getNewTypeID(), types.get(i).getNewTypeName());
            }
        }
        return type;
    }
    //Get all new technology

    public List<News> getAllByNewType1() {
        newsList = newsSession.getAllByNewsTypeID(Integer.valueOf(1));
        return newsList;
    }

    //Get all new sofware
    public List<News> getAllByNewType2() {
        newsList = newsSession.getAllByNewsTypeID(2);
        return newsList;
    }

    //Get all news
    public List<News> getAllByNewType3() {
        newsList = newsSession.getAllByNewsTypeID(Integer.valueOf(3));
        return newsList;
    }

    //delete the new
    public void delete() {
        newsSession.remove(selectNews);
    }

    //back navigation
    public void back() {
        //Check the exist of session loaddepartment
        if (SessionManage.getValue("loadNews") != null) {
            SessionManage.remove("loadNews");
        }
        URLManage.sendRediret("Admin/ManageNews.xhtml");
    }
    //Update or edit a department

    public void update() {

        selectNews.setNewTypeID(new NewType(selectNews.getNewTypeID().getNewTypeID()));
        selectNews = newsSession.updateNew(selectNews);
        if (SessionManage.getValue("loadNews") != null) {
            SessionManage.remove("loadNews");
        }
    }

    //view detail the new by new id
    public void viewDetails(Integer id) {
        try {
            //Set
            FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/faces/Employee/ViewNew.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(NewsBean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    //get the list of new
    public List<News> getNewss() {
        if (SessionManage.getValue("loadNews") == null) {
            listNews = newsSession.getAll();
            SessionManage.setValue("loadNews", "loades");
        }
        return listNews;
    }

    //create new
    public void createNews() {
        boolean result = false;

        newNews.setImage(file.getFile().getFileName());
        file.upLoad();
        newNews.setDateGenerate(new Date());
        newNews.setNewTypeID(new NewType(typeNewid));
        result = newsSession.createNewNews(newNews);
        if (result) {
            MessageManage.message(FacesMessage.SEVERITY_INFO, "Create successful!", "Successful!");

        } else {
            MessageManage.message(FacesMessage.SEVERITY_ERROR, "Cant not insert because this department is exist", "error!");
        }
        newNews = new News();
        if (SessionManage.getValue("loadNews") != null) {
            SessionManage.remove("loadNews");
        }
    }

    public NewsBean() {
        selectNews.setNewTypeID(new NewType(1));
    }
    
    public void viewNew() {
        System.out.println("-----------------------------------------"+selectNews.getNewID());
        SessionManage.setValue("newID", selectNews.getNewID());
        URLManage.sendRediret("Employee/ViewNew.xhtml");
    }
    
//    public News viewNewByID(){
//       System.out.println("4444444444444444444444"+selectNews.getNewID());  
//        return newsSession.getAllByNewsID(selectNews.getNewID());
//    }
    
    public void toTech(){
        URLManage.sendRediret("Employee/NewTech.xhtml");
    }
    
    public void toSof(){
        URLManage.sendRediret("Employee/NewSofware.xhtml");
    }
    
    public void toNews(){
        URLManage.sendRediret("Employee/News.xhtml");
    }
    
    public Integer change(double number){
       return (int)number;
    }
}
